package com.aqie.meet.ui.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.aqie.libcommon.app.PresenterToolbarActivity;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.libfactory.presenter.account.LoginContract;
import com.aqie.libfactory.presenter.account.LoginPresenter;
import com.aqie.meet.MainActivity;
import com.aqie.meet.MeetApplication;
import com.aqie.meet.R;
import com.aqie.meet.utils.ButtonUtils;
import com.aqie.meet.widget.CodeCount;

import net.qiujuer.genius.ui.widget.Loading;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class LoginActivity extends PresenterToolbarActivity<LoginContract.Presenter>
        implements LoginContract.View{
    private static final String TAG = "LOGIN";
    @BindView(R.id.bt_do_login)
    Button mButtonLogin;

    @BindView(R.id.et_phone)
    EditText mPhone;
    @BindView(R.id.et_code)
    EditText mCode;

    @BindView(R.id.tv_code)
    Button mGetCode;

    @BindView(R.id.loading)
    Loading mLoading;

    @BindView(R.id.privacy_policy_btn)
    TextView mPrivacy;

    private String mVerificationCode;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
    }

    public static void show(Context context) {
        if (context == null)
            return;
        Intent intent = new Intent(context, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    @OnClick(R.id.bt_do_login)
    public void doLogin(){
        if (ButtonUtils.isFastDoubleClick(R.id.bt_do_login)) {
            //写你相关操作即可
            return;
        }
        String phone = mPhone.getText().toString();
        String code = mCode.getText().toString();
        Log.e(TAG, phone + ":" + code);
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
            showError("手机号或验证码不能为空！");
            return;
        }
        // 调用P层进行注册
        mPresenter.login(phone, code, MeetApplication.registrationID);
        // startActivity(new Intent(this, MainActivity.class));
        // MainActivity.show(this);
    }

    CodeCount count = null;
    @OnClick(R.id.tv_code)
    public void sendSms(){
        if (TextUtils.isEmpty(mPhone.getText().toString())) {
            showError("手机号不能为空！");
            return;
        }
        count = new CodeCount(mGetCode, 60000, 1000);
        count.start();

        RemoteService service = Network.remote();
        // 得到一个Call
        Call<RspModel<String>> call = service.smsLogin(mPhone.getText().toString(), 1);
        call.enqueue(new Callback<RspModel<String>>() {
            @Override
            public void onResponse(Call<RspModel<String>> call, Response<RspModel<String>> response) {
                if(response.body()!=null && response.body().success()){
                    Log.e(TAG, "sms:"+ response.body().getData());
                    mVerificationCode = response.body().getData();
                    showError("验证码发送成功");
                } else {
                    if(response.body() != null){
                        showError(response.body().getMsg());
                    }else{
                        showError("验证码发送失败");
                    }
                }
            }

            @Override
            public void onFailure(Call<RspModel<String>> call, Throwable t) {
                Log.e(TAG, t.getMessage());
                showError("验证码发送失败");
            }
        });
    }


    @Override
    protected LoginContract.Presenter initPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void showError(String errMsg) {
        super.showError(errMsg);
        mLoading.stop();
        mPhone.setEnabled(true);
        mCode.setEnabled(true);
        mButtonLogin.setEnabled(true);
    }



    @Override
    public void loginSuccess() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (count != null) {
            count.cancel();
        }
    }

    @OnClick(R.id.privacy_policy_btn)
    void privacy(){
        Intent intent = new Intent(this, PrivacyActivity.class);
        startActivity(intent);
    }
}